define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            let vue = new Vue({
                el: '#vue',
                data: {
                    groupList: [],
                    loadingText: '拼命加载中...',
                    formLoading: false,
                    // 添加编辑
                    title: '',
                    addDialog: false,
                    // 表格当前页数据
                    tableData: [],
                    // 多选数组
                    multipleSelection: [],
                    // 搜索条件
                    key: '',
                    // 默认每页数据量
                    pageSize: 20,
                    // 当前页码
                    currentPage: 1,
                    // 默认数据总数
                    totalCount: 0,
                    // 列表加载遮罩
                    listLoading: false,
                    // 搜索字段
                    search: {
                        startTime: '',
                        endTime: '',
                        keyword: '',
                        type: '',
                        status: '',
                        sort: 'id',
                    },
                    // 字段信息
                    formData: {},
                    searchOpt: [{
                        id: 1,
                        value: '标题'
                    }],
                    statusOpt: [{
                        id: 1,
                        value: '显示'
                    }, {
                        id: 2,
                        value: '隐藏'
                    }],
                    // 表单验证规则
                    rules: {
                        title: [{
                            required: true,
                            message: '请您输入学校标题',
                            trigger: 'blur'
                        }],
                    },
                    sendStartTime: '',
                    sendEndTime: '',
                    // 时间格式化
                    dateFormat: function (row, column) {
                        var date = row[column.property];
                        if (date === undefined || date === 0) {
                            return '';
                        }
                        return moment(date * 1000).format("YYYY-MM-DD HH:mm:ss");
                    }
                },
                created() {
                    this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                },
                methods: {
                    // 刷新页面
                    refreshPage() {
                        this.search = {
                            startTime: '',
                            endTime: '',
                            keyword: '',
                            type: '',
                            status: '',
                            sort: 'id',
                        };
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    handleClearSelect(field) {
                        // 重新加载
                        this.$set(this.search, field, '');
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    handleClearSearchInp() {
                        if (this.search.type) {
                            // 重新加载
                            this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                        }
                    },
                    // 提交
                    submit() {
                        this.$refs.form.validate(valid => {
                            if (!valid) {
                                return false;
                            }

                            let formData = this.formData;

                            if (!this.formData.params.length) {
                                this.$message.warning('请您添加参数');
                                return;
                            }

                            for (let item of this.formData.params) {
                                if (!item.title || !item.content) {
                                    this.$message.warning('请您填写完整的参数内容');
                                    return;
                                }
                            }

                            // 提交
                            let loading = this.$loading({
                                lock: true,
                                text: '正在提交中...',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'shopro/goods/params/add',
                                type: 'POST',
                                data: formData,
                                success: res => {
                                    loading.close();
                                    if (res.code === 200) {
                                        this.$message.success(res.msg);
                                        this.addDialog = false;
                                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                }
                            })
                        });
                    },
                    // 状态改变
                    statusChange(row) {
                        // 提交
                        let loading = this.$loading({
                            lock: true,
                            text: '正在更改中...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.3)'
                        });
                        $.ajax({
                            url: 'shopro/goods/params/status',
                            data: {
                                id: row.id,
                                status: row.status
                            },
                            type: 'GET',
                            success: res => {
                                loading.close();
                                if (res.code === 200) {
                                    this.$message.success(res.msg);
                                    let index = this.tableData.findIndex(v => v.id == row.id);
                                } else {
                                    this.$message.warning(res.msg);
                                }
                            },
                            error: function (er) {

                            }
                        });
                    },
                    addParams() {
                        this.formData.params.push({
                            title: '',
                            content: ''
                        })
                    },
                    delParams(index) {
                        this.formData.params.splice(index, 1)
                    },
                    handleCopy(id) {
                        this.$confirm('您确定要复制此模板吗？', '温馨提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'info'
                        }).then(() => {
                            let loading = this.$loading({
                                lock: true,
                                text: '正在复制中...',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.getJSON('shopro/goods/params/copy', {
                                id,
                            }, data => {
                                loading.close();
                                if (data.code === 200) {
                                    this.$message.success(data.msg);
                                    this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                                } else {
                                    this.$message.warning(data.msg);
                                }
                            });
                        }).catch(() => {

                        });
                    },
                    // 添加/编辑
                    openAddDialog(type, item) {
                        this.addDialog = true;
                        if (type === 'add') {
                            this.title = '添加';
                            this.formData = {
                                id: 0,
                                title: '',
                                params: [],
                                status: '1',
                            };
                        } else {
                            this.title = '编辑';
                            this.formData = {
                                id: item.id,
                                title: item.title,
                                params: JSON.parse(item.params),
                                status: item.status.toString(),
                            };
                        }
                    },
                    // 从服务器读取数据
                    loadData(key, currentPage, pageSize, sort) {
                        this.listLoading = true;
                        $.getJSON('shopro/goods/params/index', {
                            keyword: key,
                            type: this.search.type,
                            start_time: this.sendStartTime,
                            end_time: this.sendEndTime,
                            status: this.search.status,
                            page: currentPage,
                            rows: pageSize,
                            sort: sort,
                        }, data => {
                            this.totalCount = data.total;
                            this.tableData = data.list;
                            this.groupList = data.groupList;
                            this.listLoading = false;
                        });
                    },
                    // 排序
                    sortChange(e) {

                    },
                    // 搜索
                    searchData() {
                        this.sendStartTime = this.search.startTime !== '' ? +this.search.startTime / 1000 : '';
                        this.sendEndTime = this.search.endTime !== '' ? +this.search.endTime / 1000 : '';
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                        this.listLoading = false;
                    },
                    // 每页显示数据量变更
                    handleSizeChange(val) {
                        this.pageSize = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 页码变更
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 多选响应
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                    // 批量删除
                    del() {
                        if (this.multipleSelection.length === 0) {
                            this.$message.warning('请选择至少一个参数模板进行删除！');
                            return;
                        }
                        var array = [];
                        this.multipleSelection.forEach((row) => {
                            array.push(row.id);
                        });

                        this.$confirm('您确定要删除所选的参数模板吗？', '温馨提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            let loading = this.$loading({
                                lock: true,
                                text: '正在删除中..',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'shopro/goods/params/dell',
                                data: "id=" + array,
                                type: "POST",
                                dataType: 'json',
                                success: res => {
                                    loading.close();
                                    if (res.code === 200) {
                                        this.$message.success(res.msg);
                                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                },
                                error: function (er) {

                                }
                            });
                        }).catch(() => {

                        });
                    },
                    // 单条删除
                    dell(id) {
                        this.$confirm('您确定要删除此参数模板吗？', '温馨提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            let loading = this.$loading({
                                lock: true,
                                text: '正在删除中..',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'shopro/goods/params/dell',
                                data: {
                                    id
                                },
                                type: "POST",
                                dataType: 'json',
                                success: res => {
                                    loading.close();
                                    if (res.code === 200) {
                                        this.$message.success(res.msg);
                                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                },
                                error: function (er) {

                                }
                            });
                        }).catch(() => {

                        });
                    },
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
